package vmcs.ui.interfacecontrol;

import vmcs.controller.MaintenanceController;
import vmcs.simulationsystem.*;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class JavaMaintenancePanelBuilder extends MaintenancePanelBuilder {
	private MaintenanceSimulationPanel maintenanceSimulationPanel;
    String TITLE = "Maintenance Panel";
    Panel   northPanel = new Panel();
    Panel   southPanel = new Panel();
    
	public JavaMaintenancePanelBuilder(ISimulatorPanel startUpPanel, MaintenanceController maintenanceController){
		super(startUpPanel, maintenanceController);
		maintenanceSimulationPanel = new MaintenanceSimulationPanel();
	}

	@Override
	public void buildAuthenticateSystem(){
        // north part
		Label     lb = new Label(TITLE);
		lb.setFont(new Font ("Helvetica", Font.BOLD, 24));
		Panel    tp1 = new Panel();
		tp1.add(lb);

		northPanel.setLayout(new GridLayout(0,1));
		this.maintenanceSimulationPanel.setPassword(new LabelledDisplay("Password:", 30, LabelledDisplay.FLOW));
        this.maintenanceSimulationPanel.getPassword().setEchoChar('*');
        PasswordListener  pl = new PasswordListener(maintenanceController.getAccessManager());
        this.maintenanceSimulationPanel.getPassword().addListener(pl);
        
        Panel tp3 = new Panel();
        this.maintenanceSimulationPanel.setValidPswd(new WarningDisplay("Valid Password"));
        this.maintenanceSimulationPanel.setInvalidPswd(new WarningDisplay("Invalid Password"));
        tp3.add(this.maintenanceSimulationPanel.getValidPswd());
        tp3.add(this.maintenanceSimulationPanel.getInvalidPswd());
        northPanel.add(tp1);
        northPanel.add(this.maintenanceSimulationPanel.getPassword());
        northPanel.add(tp3);
	}

	@Override
	public void builtCoinDisplay(){
        this.maintenanceSimulationPanel.setcDisplay(new CoinDisplay(maintenanceController));
        
        southPanel.setLayout(new GridLayout(0,1));

         this.maintenanceSimulationPanel.setTotalCash(new ButtonItem("Show Total Cash Held", ButtonItem.FLOW));
         TotalCashButtonListener   tl;

         tl = new  TotalCashButtonListener(maintenanceController);
         this.maintenanceSimulationPanel.getTotalCash().addListener(tl);

         this.maintenanceSimulationPanel.setTransferCash(new Button ("Press to Collect All Cash"));
         this.maintenanceSimulationPanel.getTransferCash().addActionListener(new TransferCashButtonListener(maintenanceController));

         Panel  tp6 = new Panel();
         tp6.setLayout(new FlowLayout());
         tp6.add(this.maintenanceSimulationPanel.getTransferCash());

         this.maintenanceSimulationPanel.setCollectCash(new LabelledDisplay("Collect Cash:", 5, LabelledDisplay.FLOW));
         this.maintenanceSimulationPanel.setExitBtn(new Button("Press Here when Finished"));
         this.maintenanceSimulationPanel.getExitBtn().addActionListener(new ExitButtonListener(maintenanceController));
         this.maintenanceSimulationPanel.getExitBtn().setFont(new Font ("Helvetica", Font.BOLD, 12));
         southPanel.add(this.maintenanceSimulationPanel.getTotalCash());
         southPanel.add(tp6);
         southPanel.add(this.maintenanceSimulationPanel.getCollectCash());

         Panel  tpBtn = new Panel();
         tpBtn.add(this.maintenanceSimulationPanel.getExitBtn());

         southPanel.add(tpBtn);
	}

	@Override
	public void builtDrinkDisplay(){
        this.maintenanceSimulationPanel.setdDisplay(new DrinkDisplay(maintenanceController));		
	}
	
	@Override
	public void buildMaintenancePanel(){
		this.maintenanceSimulationPanel.setDialog(new Dialog(((SimulatorControlPanel)this.startUpPanel).getFrame(), TITLE, false));

        // center part
        Panel  tpc = new Panel();
        tpc.setLayout(new GridLayout(0,1));
     
        tpc.setLayout(new BorderLayout());
        Panel   pp = new Panel();
        pp.setLayout(new GridLayout(1,2));
        pp.add(this.maintenanceSimulationPanel.getcDisplay());
        pp.add(this.maintenanceSimulationPanel.getdDisplay());
        tpc.add("Center", pp);
        tpc.add("South", southPanel);

        this.maintenanceSimulationPanel.getDialog().setLayout(new BorderLayout());
        this.maintenanceSimulationPanel.getDialog().add("North", northPanel);
        this.maintenanceSimulationPanel.getDialog().add("Center", tpc);

        this.maintenanceSimulationPanel.getDialog().pack();
        this.maintenanceSimulationPanel.getDialog().setLocation(200, 100);


        this.maintenanceSimulationPanel.getDialog().addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
                if(maintenanceController.closeDown() == false)
                  return;
               else
            	   maintenanceController.logoutMaintainer();
            }
	});
	}

	@Override
	public IMaintenancePanel getMaintenancePanel() {
		return maintenanceSimulationPanel;
	}
}
